package Jsoup;

import Data.Item;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) throws IOException {

        String Path = "C:\\Users\\User\\Desktop\\Asus.html";
        String URL = "http://www.chiptec.net/componentes-para-computadores/cooling/coolers-para-cpu/nox-cooler-cpu-hummer-h-100.html";
        File f = new File(Path);
        Document doc;//= Jsoup.connect(URL).get();
        //Document doc = Jsoup.parse(f, "UTF-8", URL);
        //Elements title = doc.select(".DivHeaderProdutos .ProdutoTituloDetalhe");
        //Elements price = doc.select("#price.ProdutoTituloDetalhe");
        //Elements desc = doc.select("#prodForm div:nth-child(4)");
        //Elements specs = doc.select("#productSpecification");

        doc = Jsoup.connect(URL).get();

        //Elements linksToCrawl = doc.select("div.productLists a[href]");
        //Set<String> set = new LinkedHashSet<String>();
        //System.out.println(linksToCrawl.attr("href").toString());
        parseHTMLforProducts(doc, URL);

        //System.out.println(title.text());
        //System.out.println(desc.text());
        //System.out.println(price.text());
        //System.out.println(specs.text());
    }

    private static Set<Item> parseHTMLforProducts(Document doc, String url) {
        Elements prodTitle = doc.select("div.prod_tit");
        Elements prodDesc = doc.select("div.product-name");
        Elements prodRef = doc.select("div.sku");
        Elements prodPrice = doc.select("div.product-essential span.price");
        Set<Item> items = new HashSet<Item>();
        for (int i = 0; i < prodRef.size(); i++) {
            System.out.println(prodPrice.get(i).text().replace("€", "").replace(",", "."));
            Item item = new Item(prodTitle.get(i).text(), prodRef.get(i).text(), prodDesc.get(i).text(), Double.parseDouble(prodPrice.get(i).text().replace("€", "").replace(",", ".").replace((char) 160, (char) 32)), url);
            System.out.println(item);
        }
        return items;
    }
}
